package com.ctg.behavior.calc.impala.transfer;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtSQLTransfer;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcCondition;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.common.consts.CtFunctionConsts;
import com.ctg.behavior.common.consts.CtPropertyConsts;
import com.ctg.behavior.common.consts.CtRelationConsts;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtConditionTransfer implements CtSQLTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtCalcCondition)) {
			log.warn("calcObject is not CtCalcCondition, class={}", calcObject.getClass());
			return "";
		}
		CtCalcCondition condition = (CtCalcCondition) calcObject;
		log.info("condition={}", JSON.toJSONString(condition));

		String function = condition.getFunction();
		String field = condition.getField();
		CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(field);
		field = valueType.getProperty();
		// 处理情况："field": "event._Anything.event_id"
		if ("event_id".equalsIgnoreCase(field)) {
			field = CtFieldConsts.FIELD_KEY_EVENT;
		}
		List<String> params = condition.getParams();
		StringBuilder impalaSQL = new StringBuilder();
		impalaSQL.append(" ( ");

		if (CtPropertyConsts.PROPERTY_EVENT.equalsIgnoreCase(valueType.getType())) {
			// 若是事件属性：指定事件表别名
			if (StringUtils.isEmpty(sqlParam.getParam(CtSQLConsts.KEY_EVENT_ALIAS))) {
				field = CtCalcSQLUtils.fillAlias(field, sqlParam);
			} else {
				field = CtCalcSQLUtils.fillEventAlias(field, sqlParam);
			}
		} else if (CtPropertyConsts.PROPERTY_USER.equalsIgnoreCase(valueType.getType())) {
			// 若是用户属性：指定用户表别名
			if (StringUtils.isEmpty(sqlParam.getParam(CtSQLConsts.KEY_USER_ALIAS))) {
				field = CtCalcSQLUtils.fillAlias(field, sqlParam);
			} else {
				field = CtCalcSQLUtils.fillUserAlias(field, sqlParam);
			}
		} else {
			// 如果字段有别名，则给字段加上别名前缀
			field = CtCalcSQLUtils.fillAlias(field, sqlParam);
		}

		if (params.isEmpty()) {

			if (CtFunctionConsts.KEY_ISSET.equals(function)) {
				impalaSQL.append(field).append(" is not null ");
			}
			if (CtFunctionConsts.KEY_NOTSET.equals(function)) {
				impalaSQL.append(field).append(" is null");
			}
			if (CtFunctionConsts.KEY_ISTRUE.equals(function)) {
				impalaSQL.append(field).append(" =1 ");
			}
			if (CtFunctionConsts.KEY_ISFALSE.equals(function)) {
				impalaSQL.append(field).append(" =0");
			}
		}

		for (int index = 0; index < params.size(); index++) {
			impalaSQL.append(field);
			if (CtFunctionConsts.KEY_EQUAL.equals(function)) {
				impalaSQL.append("= '");
				impalaSQL.append(params.get(index));
				impalaSQL.append("'");
			}
			if (CtFunctionConsts.KEY_NOTEQUAL.equals(function)) {
				impalaSQL.append("<> '");
				impalaSQL.append(params.get(index));
				impalaSQL.append("'");
			}
			if (CtFunctionConsts.KEY_LESS.equals(function)) {
				impalaSQL.append("<");
				impalaSQL.append(params.get(index));
			}
			if (CtFunctionConsts.KEY_GREATER.equals(function)) {
				impalaSQL.append(">");
				impalaSQL.append(params.get(index));
			}
			if (CtFunctionConsts.KEY_CONTAIN.equals(function)) {
				impalaSQL.append(" like %").append(params.get(index)).append("%");
			}
			if (CtFunctionConsts.KEY_NOTCONTAIN.equals(function)) {
				impalaSQL.append(" not like %").append(params.get(index)).append("%");
			}

			if (index == params.size() - 1) {
				continue;
			}
			impalaSQL.append(" ").append(CtRelationConsts.KEY_OR).append(" ");
		}
		impalaSQL.append(" ) ");

		String retSQL = impalaSQL.toString();
		if (CtCalcSQLUtils.isEmptyCondtions(retSQL)) {
			retSQL = "";
		}
		return retSQL;
	}

}
